Alternative Monads are monadic types that can have an alternative value! What does this mean?
If first we think about a tuple:
(int, string)
This type can represent an int
AND a string
. Now consider the Either<L, R>
monad, this means the value
can beLeft
OR Right
(L
or R
).
So, this:
Either<int, string>
Means either int
OR string
. It is the natural dual of tuple.
In the case of Either
the Right value is considered the bound value of the monad, and the Left value
is considered the alternative value. All of the other alternative value monads can be seen as derivatives
or specialisations of Either
.
Type | Alternative Value Type | Bound Value Type | Notes |
---|---|---|---|
Either<L, R> |
L |
R |
|
Option<A> |
None |
A |
Equivalent to Either<Unit, A> |
Fin<A> |
Error |
A |
Equivalent to Either<Error, A> |
Validation<Fail, Succ> |
Fail |
Succ |
Fail must be a Monoid<Fail> to collect errors |
Nullable<A> |
null |
A |
Equivalent to Either<Unit, A> |
The alternative value is usually used to carry errors, but that doesn't have to be the case. It is important to remember that the alternative-value can carry anything you want.